const express = require("express");
const Mock = require("mockjs");
const app = express();
const port = 3000;
//中间件,为req提供body属性获得参数
const bodyParser = require("body-parser");
// 处理静态资源
app.use(express.static("public"));
// 处理请求参数
// bodyParser提供接受json请求参数的支持
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// 设置允许跨域访问该服务
app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials"
  );
  res.header("Access-Control-Allow-Credentials", "true");
  if (req.method == "OPTIONS") {
    res.sendStatus(200);
  } else {
    next();
  }
});

app.get("/", (req, res) => res.send("Hello World!"));

app.get("/hello", (req, res) => res.send("Hello Nodemon!"));

// jquery的Ajax请求
app.get("/jq01", (req, res) => {
  setTimeout(function () {
    res.send("服务器响应数据: jq-01");
  }, 100);
});
app.get("/jq02", (req, res) => {
  setTimeout(function () {
    res.send("服务器响应数据: jq-02");
  }, 1000);
});
app.get("/jq03", (req, res) => {
  setTimeout(function () {
    res.send("服务器响应数据: jq-03");
  }, 500);
});

//fetch的ajax请求
app.get("/f01", (req, res) => res.send("Hello Fetch!"));
app.post("/f02", (req, res) => res.send("Hello post Fetch!"));
//fecth的restfull的案例
app.get("/books", (req, res) => res.send("传统的GET传参:id="+req.query.id));
app.get("/books/:id", (req, res) => res.send("RestFul的GET传参:id="+req.params.id));
app.delete("/books/:id", (req, res) => res.send("RestFul的delete传参:id="+req.params.id));
app.post("/books", (req, res) => res.send("传统的post传参:uname="+req.body.uname+",pwd="+req.body.pwd));
app.put("/books/:id", (req, res) => res.send("put传参 id="+req.params.id+",uname="+req.body.uname+",pwd="+req.body.pwd));

//axios的请求
app.get("/a01", (req, res) => res.send("Hello Axios!"));
//axios的restful请求
app.get("/axios", (req, res) => res.send("传统的GET传参:id="+req.query.id));
app.get("/axios/:id", (req, res) => res.send("RestFul的GET传参:id="+req.params.id));

//async测试
app.get("/async01", (req, res) => {
  setTimeout(function () {
    res.send("haoren");
  }, 300);
});

app.get("/async02", (req, res) => {
  setTimeout(function () {
    res.send("服务器响应数据"+ req.query.info);
  }, 200);
});

//jquery的Ajax请求

app.get("/mock", function (req, res) {
  /**
   * mockjs中属性名‘|’符号后面的属性为随机属性，数组对象后面的随机属性为随机数组数量，正则表达式表示随机规则，+1代表自增
   */
  res.json(
    Mock.mock({
      status: 200,
      "data|1-9": [
        {
          "name|5-8": /[a-zA-Z]/,
          "id|+1": 1,
          "value|0-500": 20,
        },
      ],
    })
  );
});

// 拓展mockjs
Mock.Random.extend({
  phone: function () {
    var phonePrefixs = ["132", "135", "189", "177", "188"]; // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/); //Number()
  },
});

app.get("/emps", function (req, res) {
  res.json(
    Mock.mock({
      status: 200,
      "data|5-10": [
        {
          "id|+1": 1001,
          name: "@CNAME", //随机生成一个中文的姓名
          idcard: "@ID", //id必须大写,生成一个18位的身份证号码
          hiredate: "@DATE", //入职时间
          address: "@CITY(true)",
          email: "@EMAIL",
          phone: "@PHONE",
          "salary|4000-20000": 1,
        },
      ],
    })
  );
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
